/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.commons.tools;

import java.util.UUID;

public class GuidGenerator {

    private String currentGuid = null;

    public GuidGenerator() {
        this.currentGuid = generateGuid();
    }

    public static String getThreadLocalGuid() {
        return threadLocalGuidGenerator.get().currentGuid;
    }

    public static String generateThreadLocalGuid() {
        String guid = generateGuid();
        threadLocalGuidGenerator.get().currentGuid = guid;
        return guid;
    }

    public static String generateGuid() {
        return UUID.randomUUID().toString().toUpperCase();
    }

    private static ThreadLocal<GuidGenerator> threadLocalGuidGenerator = new ThreadLocal<GuidGenerator>() {
        protected synchronized GuidGenerator initialValue() {
            return new GuidGenerator();
        }
    };
}
